package ywpt.service.config;

import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.KnowledgeType;
import ywpt.web.common.PublicFunction;
@Service("knowledgeTypeService")
@RemoteProxy(name="knowledgeTypeService")
public class KnowledgeTypeServiceImpl  extends HibernateEntityDao<KnowledgeType> implements KnowledgeTypeService{
	public KnowledgeTypeServiceImpl(){
		
	}

	@Override
	public KnowledgeType getKnowledgeTypeById(Long id) {
		return this.get(id);
	}

	@Override
	@RemoteMethod
	public String creatKnowledgeTypeCode(Long parentId) {
		DetachedCriteria query = DetachedCriteria.forClass(KnowledgeType.class);
		String typeCode="";
		String code="";
		String parentCode="";
		Integer maxId=new Integer(0);
		if(parentId!=null){
			parentCode = this.get(parentId).getTypeCode();
			query.createAlias("parent", "parent").add(Restrictions.eq("parent.id", parentId));
		}else{
			query.add(Restrictions.isNull("parent"));
		}
		query.addOrder(Order.desc("id"));
		List<KnowledgeType> knowledgeTypes = this.getKnowledgeTypes(query);
		if ( knowledgeTypes.iterator().hasNext() )	
		{
			code = knowledgeTypes.iterator().next().getTypeCode().substring(parentCode.length());	
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		return parentCode+typeCode;
	}
	
	
	@Override
	public int getKnowledgeTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<KnowledgeType> getKnowledgeTypes(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<KnowledgeType> getKnowledgeTypes() {
		return this.getAll();
	}

	@Override
	public void removeKnowledgeType(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveKnowledgeType(KnowledgeType knowledgeType,Long parentId) {
		if(parentId!=null)
			knowledgeType.setParent(this.get(parentId));
		this.save(knowledgeType);
	}

	@Override
	public void updateKnowledgeType(KnowledgeType knowledgeType,Long parentId) {
		if(parentId!=null)
			knowledgeType.setParent(this.get(parentId));
		this.update(knowledgeType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartment() {
		String hql="select distinct(depart) from KnowledgeType";
		return this.find(hql);
	}

	



	@SuppressWarnings("unchecked")
	@Override
	public List<KnowledgeType> getKnowledgeTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<KnowledgeType> getChildrenByParent(Long parentId) {
		DetachedCriteria query = DetachedCriteria.forClass(KnowledgeType.class);
		query.createAlias("parent", "parent").add(Restrictions.eq("parent.id", parentId));
		return this.findAllByCriteria(query);
	}

	@Override
	@RemoteMethod
	public String getTypeNameByIds(Long first, Long second, Long third) {
		String result="";
		if(this.get(first)!=null)
			result = this.get(first).getTypeName();
		if(second!=null&&this.get(second)!=null)
			result += "-->>"+this.get(second).getTypeName();
		if(third!=null&&this.get(third)!=null)
			result += "-->>"+this.get(third).getTypeName();
		return result;
	}

}
